//
//  SYAFNTools.swift
//  SYWeibo
//
//  Created by Saya on 16/4/9.
//  Copyright © 2016年 黄金果. All rights reserved.
//

import UIKit
import AFNetworking

struct API {
    static let token = "oauth2/access_token"
    static let userinfo = "2/users/show.json"
    static let homeData = "2/statuses/home_timeline.json"
    static let sendTextWeibo = "2/statuses/update.json"
    static let sendWeiboWithPicture = "https://upload.api.weibo.com/2/statuses/upload.json"//如果有host的话,那么baseURL不能被使用
}

enum SYHTTPModth : String {
    case POST = "POST"
    case GET = "GET"
}

class SYAFNTools: AFHTTPSessionManager {
    //创建一个单例对象供外界使用
    static let shareTools:SYAFNTools = {
        let tempTool = SYAFNTools(baseURL: NSURL(string: "https://api.weibo.com/"))
        tempTool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tempTool
    }()


    
}

//创建一个分类,获取home的微博数据
extension SYAFNTools{
    func loginHomeData(accessToken:String,max_id:Int64,since_id:Int64,finishBlock:(reslut:AnyObject?,error:NSError?)->Void){
        let urlString = API.homeData
        var para:[String:AnyObject] = ["access_token":accessToken]
        if max_id > 0{
            para["max_id"] = "\(max_id-1)"
        }
        if since_id > 0{
            para["since_id"] = "\(since_id)"
        }
        
        print(para)
        request(.GET, URLString: urlString, parameters: para, finishBlock: finishBlock)
    }
    
    func sendWeibo(accessToken:String,status:String,images:[UIImage]=[UIImage](),finishBlock:(reslut:AnyObject?,error:NSError?)->Void){
        
        let para:[String:AnyObject] = ["access_token":accessToken,"status":status]
        
        if images.count > 0 {
            self.POST(API.sendWeiboWithPicture, parameters: para, constructingBodyWithBlock: { (formate) -> Void in
                for (index,tempImg) in images.enumerate(){
                    let imageData:NSData = UIImageJPEGRepresentation(tempImg, 0.5)!//如果支持gif/jpg/png需要怎么做
                    formate.appendPartWithFileData(imageData, name: "pic", fileName: "dd\(index)", mimeType: "image/jpeg")
                }
                }, progress: nil, success: { (_, body) -> Void in
                    finishBlock(reslut: body, error: nil)
                }) { (_, error) -> Void in
                    finishBlock(reslut: nil, error: error)
            }
            
        }else{//这种情况下是存文本日志
            request(.POST, URLString: API.sendTextWeibo, parameters: para, finishBlock: finishBlock)
        }
    }

}

extension SYAFNTools {
    //提供一个方法获取用户的token
    func loginUserToken(code:String,finishBlock:(reslut:AnyObject?,error:NSError?)->Void){
        let urlString :String = API.token
        let para :[String:AnyObject] = [CLIENT_ID:client_id,CLIENT_SECRET:secret,GRANT_TYPE:"authorization_code",CODE:code,REDIRECT_URI:redirect_uri]
        request(.POST, URLString: urlString, parameters: para, finishBlock: finishBlock)
    }
    
    //提供一个方法获取用户的数据
    func loginUserInfo(reasponse:[String:AnyObject],finishBlock:(reslut:AnyObject?,error:NSError?)->Void){
        let urlString:String = API.userinfo
        let para:[String:AnyObject] = ["access_token":reasponse["access_token"]!,"uid":reasponse["uid"]!]
        request(.GET, URLString: urlString, parameters: para, finishBlock: finishBlock)
    }
}


extension SYAFNTools {
    //封装一个方法,给内部使用基本网络请求
    func request(httpMothed:SYHTTPModth ,URLString: String, parameters: AnyObject?,finishBlock:(reslut:AnyObject?,error:NSError?)->Void){
        
        let succeedBlock = {(task:NSURLSessionDataTask, reasponse:AnyObject?) -> Void in
            finishBlock(reslut: reasponse, error: nil)
        }
        let failureBlock = {(task :NSURLSessionDataTask?, error:NSError) -> Void in
            finishBlock(reslut: nil, error: error)
        }
        
        if httpMothed == .GET{
            GET(URLString, parameters: parameters, progress: nil, success: succeedBlock, failure: failureBlock)
        }else{
            POST(URLString, parameters: parameters, progress: nil, success: succeedBlock, failure: failureBlock)
        }
        
    }
}

//对于afn的使用注意点:
/**
override func POST(URLString: String, parameters: AnyObject?, constructingBodyWithBlock block: ((AFMultipartFormData) -> Void)?, progress uploadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) -> NSURLSessionDataTask? {

}
override func POST(URLString: String, parameters: AnyObject?, progress uploadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) -> NSURLSessionDataTask? {

}

第一个方法包含constructingBodyWithBlock,不可以传递nil,需要拼接具体的值
如果不需要上传数据的话那么可以使用第二种方法
*/












